<?php
namespace Mob \ Model;

use Think \ Model;
use Think \ Hook;
/**请求模型
 * Class InfoModel
 */

require_once ('./Application/Mob/Common/function.php');

class ActivityModel extends Model {

  public function creationActivity($sponsorUid,$Title,$activityType,$actlongitude,$actlatitude,$latitude,$longitude,$positiontype,$isPay,$amount='',$people,$allotMode='3')
    // public function creationActivity($sponsorUid,$Title,$activityType,$latitude,$longitude)
     {
      $data['activityId']= getActivitySerialno();
      $data['sponsorUid']=$sponsorUid?$sponsorUid:get_uid();
      $data['Title']=$Title;
      $data['activityType']=$activityType;
      $data['latitude']=$latitude;
      $data['longitude']=$longitude;
      $data['actlongitude']=$actlongitude;
      $data['actlatitude']=$actlatitude;
      $data['isPay']=$isPay;
      $data['amount']=$amount;
      $data['allotMode']=$allotMode;
      $data['peopleNum']=$people;
      $data['deadline']=getDateTime(30);  //默认截止时间为30天。
      $data['positiontype']=$positiontype;



      if($this->add($data))
      {
      return $data['activityId'];
      }else
      {
      return null;

      }

     }




     public function releaseActivity($activityId,$sponsorUid)
     {


     $Activity = $this->queryActivity($activityId);


      if (!$Activity) return false;
      if ($Activity['status']==1) return false;

      if($Activity['isPay']==1)  //为付费
      {

      if($Activity['status']==5) return false;


       if(!D("Mob/Orders")->isPayOrder($Activity['orderId'])) return false;

      }

      $data= array('status'=>'1','releaseTime'=>getDateTime());
      $this->where(" activityId = '%s' and status <> 2 and sponsorUid = '%s' ",$activityId,$sponsorUid)->save($data);



      switch($Activity['activityType'])
      {
       case 1:

       $notice = D("Mob/Notice");


       if(empty($Activity['businessId'])) return false;

       $redpack = D("Mob/RedPack");

       $notice->releaseNotice($Activity['businessId'],$Activity['sponsorUid']);
       $redpack->payComplete($Activity['paySerialno']);
       return true;
       break;

       case 2:
       break;
      }

     }

     public function queryActivity($activityId)
     {
     $where = array('activityId'=>$activityId);

    return $this->where($where)->field('sponsorUid,createTime,Title,latitude,longitude,activityType,isPay,status,completionTime,browseCount,releaseTime,paySerialno,peopleNum,ParticipantsSum,limitCondition,deadline,closeTime,allotMode,amount,contents,maxAttendNum,orderId,businessId,positiontype')->find();


     }

     public function updateBusinessId($activityId,$businessId)
     {
     $data = array('businessId'=>$businessId);

     $this->where(" activityId = '%s' and status in (0,1,6) ",$activityId)->save($data);


     }
     public function updateActivityInfo($activityId,$data)
     {

    return $this->where(" activityId = '%s' ",$activityId)->save($data);


     }

    public function activityHall($position='',$data = '', $distance = 50000,$sortType =1)
    {

     $activity = new \ Think \ Model();
     $datetime = getDateTime();

     $latitude =$position['latitude'];
	 $longitude=$position['longitude'];



        return $activityHall = $activity->query("select * from (select round(6378.138*2*asin(sqrt(pow(sin( (".$latitude."*pi()/180-actlatitude*pi()/180)/2),2)+cos(".$latitude."*pi()/180)*cos(actlatitude*pi()/180)* pow(sin( (".$longitude."*pi()/180-actlongitude*pi()/180)/2),2)))*1000) as distance ,activityId, sponsorUid,createTime,releaseTime,Title,actlatitude,actlongitude,activityType,isPay,browseCount,paySerialno,peopleNum,positiontype,amount,businessId
 from __PREFIX__activity where status =1  and actlatitude != '' and actlongitude != '' and   releaseTime <= '".$datetime."' and deadline >  '".$datetime."') as activityHall where distance <=  ".$distance);    //unix_timestamp(releaseTime) as releaseTime





    }

   public function details($activityId)
   {
   	$where = array('activityId'=>$activityId,'status'=>'1');

   return $activity = M('Activity')->where($where)->field('sponsorUid,Title,latitude,longitude,activityType,isPay,status,browseCount,paySerialno,orderId,businessId,releaseTime,activityId,positiontype')->find();



   }

  public function cancelActivity($activityId)
  {
  	$where = array('activityId'=>$activityId);
  	$res = $this->where($where)->field('sponsorUid,status,isPay,businessId,activityType,paySerialno')->find();
  	if($res['sponsorUid']!=get_uid()||$res['status']==4)  //如果活动发布人与当前登录人员ID不符，或者任务已经取消了，则不允许取消操作。
  	{
  		return false;   //直接返回
  	}
  	$data['status']=4;
  	$data['closeTime']=getDateTime();
  	$this->where($where)->save($data);
  	if($res['isPay']==1)
  	{
  	$paySerialno = $res['paySerialno'];
  	$type = $res['activityType'];
  	$redPack = D('RedPack');
  	$redPack->closeRedPack($paySerialno);

  	}

  	switch($type)
  	{

  	 case 1:

  	 $notice = D('Notice');
  	 $notice->overNotice($res['businessId']);
  	 return true;

  	 break;

  	 case 2:
  	 break;

  	 case 3:

  	 break;

  	 case 4:
  	 break;

  	}
  }

  	public  function browseRecord()
  	{
  	 $activityId = I('actId');
  	 $Uid= get_uid();
  	 $where = array('activityId'=>$activityId,'status'=>'1');

  	 $actvity = $this->where($where)->field('activityType,sponsorUid,status,businessId')->find();
  	 $sponsorUid = $actvity['sponsorUid'];
  	 $type = $actvity['activityType'];
  	 $status = $actvity['status'];
  	  $this->where($where)->setInc('browseCount');   //增加浏览数

  	 if($sponsorUid==$Uid||$status!=1)  //如果当前浏览人为活动发布人，这样的话，不算浏览量。另外，如果活动没有发布的话，也不算。
  	 {
  	 return ;   //直接返回
  	 }

  	 if($Uid==0) //判断一下用户是否登录了，如果没有登录的话，返回
  	 {
  	 return ;
  	 }
  	 if($activityId=='')
  	 {
  	 return	 null;
  	 }
  	 switch($type)
  	 {
  	 	case 1:
  	 $notice = D('Notice');
  	 $notice->browseNotice($actvity['businessId'],$Uid);
  	 	break;

  	 	case 2:

  	 	break;

  	 	case 3:
  	 	break;



  	 }

  	}
  public function activityMap($position='',$data = '', $distance = 50000)
  {

     $activity = new \ Think \ Model();
     $datetime = getDateTime();

     $latitude =$position['latitude'];
	 $longitude=$position['longitude'];

    return $activityHall = $activity->query("select * from (select round(6378.138*2*asin(sqrt(pow(sin( (".$latitude."*pi()/180-actlatitude*pi()/180)/2),2)+cos(".$latitude."*pi()/180)*cos(actlatitude*pi()/180)* pow(sin( (".$longitude."*pi()/180-actlongitude*pi()/180)/2),2)))*1000) as distance ,activityId, sponsorUid,createTime,releaseTime,Title,actlatitude as latitude,actlongitude as longitude,isPay,peopleNum,amount,businessId,positiontype
 from __PREFIX__activity where status =1  and actlatitude != '' and actlongitude != '' and   releaseTime <= '".$datetime."' and deadline >  '".$datetime."') as activityHall where distance <=  ".$distance);


  }

 public function deleteActivity($activityId,$uid)
 {







 }
















}